<?php

class Default_Service_InterFeeCalc{
	private $_warehouseId;         //仓库ID
    private $_countryId;           //国家ID
    private $_stId;                //货运方式ID
    private $_weight;              //重量
    private $_length;              //长
    private $_width;               //宽
    private $_height;              //高
    private $_customerId;          //客户ID
    private $_stCodeClass;         //货运方式分类
    private $_stSort;              //货运方式排序方式
    private $_realWeight;          //计费重量
    private $_shipType;            //计费重量
	 
	public function __construct($array){
        if($array!=null){
			foreach($array as $key=>$val){
				$this->set('_'.$key,$val);
			}
		}
	}
    public function set($key,$value){
		$this->$key =$value;
	}
    /*
    *根据仓库ID,国家ID,货运方式ID,重量,长,宽,高,客户ID获取运费
    */
    public function getRate(){

        $this->_shipType = Default_Service_ShipType::getByValue($this->_stId);
        /*验证数据*/
		
        $filter =$this->filter();
		
        if($filter!=100)
        {
            return $filter;
        }

        /*获取计费重量*/
		$this->realWeight();

		
		/*获取费用*/
		$this->getCostRate();
        if(!is_array($this->_rate))
        {
            return $this->_rate;
        }

        /*折扣处理*/
		$this->calculateCost();

        $this->_shipType['addons_fee'] = $this->_addons;
        $this->_shipType['base_fee'] = $this->_baseFee;
        $this->_shipType['inter_fee'] = $this->_cost;
		$this->_shipType['currency_code'] =  $this->_rate['currency_code'];
		
		return $this->_shipType;
	}

    /*
    *根据仓库ID,国家ID,重量,长,宽,高,客户ID获取货运方式及运费
    */
    public function getAllRate(){
        $condition   = array('st_status' =>1);
        $shipTypeList = Default_Service_ShipType::getByCondition($condition);
        if(empty($shipTypeList)){
            return -6;          //取不到货运方式
        }
        $returnList = array();
        foreach($shipTypeList as $shipType){
            $this->_stId = $shipType['st_id'];
            $return = $this->getRate();
            if(is_array($return) &&!empty($return)){
                $returnList[] =$return;
            }
        }
        return $returnList;
    }

    /*
    *根据仓库ID,国家ID,货运方式类型,重量,长,宽,高,客户ID获取货运方式及运费
    */
	public function getByClass(){
        if(empty($this->_stCodeClass)){
            return -6;
        }

        $condition   = array(
            'st_status'     => 1,
            'st_class_code' => $this->_stCodeClass,
        );
        $shipTypeList = Default_Service_ShipType::getByCondition($condition);
        if(empty($shipTypeList)){
            return -6;          //取不到货运方式
        }
        $returnList = array();
        foreach($shipTypeList as $shipType){
            $this->_stId = $shipType['st_id'];
            $return = $this->getRate();
            if(is_array($return) &&!empty($return)){
                $returnList[] =$return;
            }
        }
        return $returnList;
	}

    /*
    *根据仓库ID,国家ID,排序类型,重量,长,宽,高,客户ID获取货运方式及运费
    */
    public function getBySortType() {
        if(empty($this->_stSort)){
            return -6;
        }

        $condition   = array(
            'st_status'     => 1,
            'st_sort' => $this->_stSort,
        );
        $shipTypeList = Default_Service_ShipType::getByCondition($condition);
        if(empty($shipTypeList)){
            return -6;          //取不到货运方式
        }
        $returnList = array();
        foreach($shipTypeList as $shipType){
            $this->_stId = $shipType['st_id'];
            $return = $this->getRate();
            if(is_array($return) &&!empty($return)){
                $returnList[] =$return;
            }
        }
        return $returnList;
    }


    /*
    *根据仓库ID,国家ID　可用的货运方式;
    */
    public function getAvailShipType(){
		//	$condition   = array(
		//		'country_id' => $this->_countryId,
		//	);
		$stcMap = new Default_Model_ShipTypeCountryMap();
        $stCodeArr = $stcMap->getAvailShipping($this->_countryId,array('st_code','stcm_area'));
        $distinctCode =array();
        $returnList = array();
		
        foreach($stCodeArr as $row){
            if(!in_array($row['st_code'],$distinctCode)){
                $distinctCode[] = $row['st_code'];
				
                $shipType = Default_Service_ShipType::getbyValue($row['st_code'],'st_code');
				 
                $this->_shipType = $shipType;	
				//$this->_area = 	$row['stcm_area'];//计费区域	
                if(empty($shipType) || $shipType['st_status']==0)
                {
					continue;
                }
                $this->_stId = $shipType['st_id'];
                $return = $this->getFeeByShipType();
                if(is_array($return) &&!empty($return)){
                    $returnList[] = $shipType;
                }
            }
        }
        return $returnList;
    }


    /*基本参数验证处理*/
    public function filter(){
        $country = Default_Service_Country::getByValue($this->_countryId);
        if(empty($country)){
            return -1;          //国家ID不存在;
        }

        $shipType = $this->_shipType;
        if(empty($shipType)){
            return -2;          //货运方式ID不存在;
        }

        if(empty($this->_weight)){
            return -3;          //重量为0或为空;
        }
        return 100;
    }


    /*获取计费重量*/
	public function realWeight(){
        $shipType = $this->_shipType;
        $this->_realWeight = $this->_weight;
        $this->_shipType['weight'] =$this->_weight;
        $this->_shipType['vol_weight'] = 0;
        if(1==$shipType['st_is_volume'])
        {
            if($shipType['st_vol_weight_rate']>0)
            {
                $vol = $this->_length*$this->_width*$this->_height;

                $vol_weight = sprintf("%.2f",$vol/$shipType['st_vol_weight_rate']);

                $this->_shipType['vol_weight'] = $vol_weight;

                if($vol_weight>$this->_weight && $vol>$shipType['st_limit_volume']){
                    $this->_realWeight = $vol_weight;
                }
            }
        }
        $this->_shipType['real_weight'] =$this->_realWeight;
    }


    /*取相对应该的基本费用*/
	private function getCostRate(){
         $shipType = $this->_shipType;
         $areaArr =array();
		 $stcMap = new Default_Model_ShipTypeCountryMap();
         $areas = $stcMap->getShippingArea($this->_countryId,$shipType['st_code'],'stcm_area');        
         $areaArr = array();
		 foreach($areas as $row){
            $areaArr[] = $row['stcm_area'];
         }  
         if(empty($areaArr)){
            $this->_rate = -4;           //货运方式国家不存在对应的区域
            return false;
         }

         $tempCondition = array(
            'st_id'        => $this->_stId,
            'country_id'   => $this->_countryId,
            'areaArr'      => $areaArr,
            'warehouse_id' => $this->_warehouseId,
            'sp_weight'    => $this->_realWeight,
         );

         $shipPrice = Default_Service_ShipPrice::getByCondition($tempCondition,'*','sp_weight,sp_cost');

         if(!isset($shipPrice[0]) ||empty($shipPrice[0])){
            $this->_rate = -5;           //货运方式国家不存在价格
            return false;
         }
         $this->_rate = $shipPrice[0];
    }


    /*货运方式，国家，仓库ID是否存在 费用*/
	private function getFeeByShipType(){
        $shipType = $this->_shipType;
                 
		$stcMap = new Default_Model_ShipTypeCountryMap();
        $areas = $stcMap->getShippingArea($this->_countryId,$shipType['st_code'],'stcm_area');        
        $areaArr = array();
		foreach($areas as $row){
            $areaArr[] = $row['stcm_area'];
        }  
		 
        if(empty($areaArr)){
            //货运方式国家不存在对应的区域
            return -4;;
        }
        $tempCondition = array(
            'st_id'        => $this->_stId,
            'country_id'   => $this->_countryId,
            'areaArr'      => $areaArr,
            'warehouse_id' => $this->_warehouseId
			);
        $shipPrice = Default_Service_ShipPrice::getByCondition($tempCondition,'*','sp_weight,sp_cost',1,1);
        if(!isset($shipPrice[0]) ||empty($shipPrice[0])){
            //货运方式国家不存在价格
            return -5;
        }else{
            return $shipPrice[0];
        }
    }

    /*运费折扣计算*/
    private function calculateCost(){
        $discount =$this->_shipType['st_discount'];
        $addons =$this->_shipType['st_addons'];
        $deal_fee =$this->_shipType['st_deal_fee'];
        $reg_fee =$this->_shipType['st_reg_fee'];
        switch(strtolower($this->_rate["sp_calc_type"]))
        {
            case 'w':
                $this->_baseFee =$this->_realWeight*$this->_rate['sp_cost'];
                $this->_baseFee =$this->_baseFee*$discount;
                break;
            case 'p':
                $this->_baseFee =$this->_rate['sp_cost']*$discount;
                break;
			case 'h':
                $this->_baseFee =$this->_realWeight*$this->_rate['sp_cost'];
                $this->_baseFee =$this->_baseFee*$discount;
                break;	
        }

        $this->_addons  =$addons*@$this->_baseFee;
        $this->_cost = @$this->_baseFee+$this->_addons;
        $this->_cost = $this->_cost+$deal_fee+$reg_fee;

        $this->_baseFee =sprintf('%.2f',@$this->_baseFee);
        $this->_addons = sprintf('%.2f',$this->_addons);
        $this->_cost = sprintf('%.2f',$this->_cost);
	}
}